<?php

include_once 'pg_conn.class.php';
include_once 'odbc_conn.class.php';
include_once 'mysql_conn.class.php';
include_once 'sqlite_conn.class.php';
include_once 'oci_conn.class.php';
include_once 'configuracion.class.php';

class Queryaction extends Configuracion {

    private $dbconn = null;
    private $conn = null;
    private $conectado = false;

    public function Queryaction($dbname) {
        switch ($this->getType($dbname)) {
            case 'oci':
                $cnf = $this->getConfiguracion($dbname);
                $this->dbconn = oci_connect($cnf['user'], $cnf['password'], $cnf['host'] . '/' .$cnf['dbname']);
                $this->conn = new Oci_conn($this->dbconn);
                if (is_resource($this->dbconn))
                    $this->conectado = true;
                break;
            case 'postgres':
                $this->dbconn = pg_connect($this->getConnection_string($dbname));
                $this->conn = new Pg_conn($this->dbconn);
                if (is_resource($this->dbconn))
                    $this->conectado = true;
                break;
            case 'odbc':
                $cnf = $this->getConfiguracion($dbname);
                $this->dbconn = ini_get('allow_persistent') ? @odbc_connect($cnf['host'], $cnf['user'], $cnf['password']) : @odbc_connect($cnf['host'], $cnf['user'], $cnf['password']);
                $this->conn = new Odbc_conn($this->dbconn);
                if (is_resource($this->dbconn))
                    $this->conectado = true;
                break;
            case 'mysql':
                $cnf = $this->getConfiguracion($dbname);
                $this->dbconn = mysql_connect($cnf['host'], $cnf['user'], $cnf['password']);
                $this->conn = new Mysql_conn($this->dbconn);
                if (is_resource($this->dbconn))
                    $this->conectado = true;
                break;
            case 'sqlite':
                $cnf = $this->getConfiguracion($dbname);
                $this->dbconn = sqlite_open($cnf['dbname'], 0666);
                $this->conn = new Sqlite_conn($this->dbconn);
                if (is_resource($this->dbconn))
                    $this->conectado = true;
                break;
            default:
                $cnf = $this->getConfiguracion($dbname);
                $this->dbconn = ini_get('allow_persistent') ? @odbc_connect($cnf['host'], $cnf['user'], $cnf['password']) : @odbc_connect($cnf['host'], $cnf['user'], $cnf['password']);
                $this->conn = new Odbc_conn($this->dbconn);
                if (is_resource($this->dbconn))
                    $this->conectado = true;
                break;
        }
    }

    public function setDbconn($dbname) {
        switch ($this->getType($dbname)) {
            case 'oci':
                $cnf = $this->getConfiguracion($dbname);
                $this->dbconn = oci_connect($cnf['user'], $cnf['password'], $cnf['host'] . '/' .$cnf['dbname']);
                $this->conn = new Oci_conn($this->dbconn);
                if (is_resource($this->dbconn))
                    $this->conectado = true;
                break;
            case 'postgres':
                $this->dbconn = @pg_connect($this->getConnection_string($dbname));
                if (is_null($this->conn))
                    $this->conn = new Pg_conn($this->dbconn);
                else
                    $this->conn->setDbconn($this->dbconn);
                if (is_resource($this->dbconn))
                    $this->conectado = true;
                break;
            case 'odbc':
                $cnf = $this->getConfiguracion($dbname);
                $this->dbconn = ini_get('allow_persistent') ? @odbc_connect($cnf['host'], $cnf['user'], $cnf['password']) : @odbc_connect($cnf['host'], $cnf['user'], $cnf['password']);
                if (is_null($this->conn))
                    $this->conn = new Odbc_conn($this->dbconn);
                else
                    $this->conn->setDbconn($this->dbconn);
                if (is_resource($this->dbconn))
                    $this->conectado = true;
                break;
            case 'mysql':
                $cnf = $this->getConfiguracion($dbname);
                $this->dbconn = mysql_connect($cnf['host'], $cnf['user'], $cnf['password']);
                $this->conn = new Mysql_conn($this->dbconn);
                if (is_resource($this->dbconn))
                    $this->conectado = true;
                break;
            case 'sqlite':
                $cnf = $this->getConfiguracion($dbname);
                $this->dbconn = @sqlite_open($cnf['dbname'], 0666);
                $this->conn = new Sqlite_conn($this->dbconn);
                if (is_resource($this->dbconn))
                    $this->conectado = true;
                break;
            default:
                $cnf = $this->getConfiguracion($dbname);
                $this->dbconn = ini_get('allow_persistent') ? @odbc_connect($cnf['host'], $cnf['user'], $cnf['password']) : @odbc_connect($cnf['host'], $cnf['user'], $cnf['password']);
                if (is_null($this->conn))
                    $this->conn = new Odbc_conn($this->dbconn);
                else
                    $this->conn->setDbconn($this->dbconn);
                if (is_resource($this->dbconn))
                    $this->conectado = true;
                break;
        }
    }

    public function close() {
        $this->conn->close($this->dbconn);
        $this->conn = null;
        $this->dbconn = null;
        $this->conectado = false;
    }

    public function executeOnly($sql) {
        $this->conn->execute($sql);
        return null;
    }

    public function executeAssoc($sql) {
        if (!$this->conectado)
            $this->exitAction();
        return $this->conn->fetchArrayAssoc($this->conn->execute($sql));
    }

    public function executeBoth($sql) {
        if (!$this->conectado)
            $this->exitAction();
        return $this->conn->fetchArrayBoth($this->conn->execute($sql));
    }

    public function executeNum($sql) {
        if (!$this->conectado)
            $this->exitAction();
        return $this->conn->fetchArrayNum($this->conn->execute($sql));
    }

    function exitAction() {
        echo 'No se pudo establecer conexion con la base de datos.';
        exit();
    }

    public function prepareInt($num) {
        return (strlen($cad) != 0 ? $cad : 'null');
    }

    public function prepareString($cad) {
        return (strlen($cad) != 0 ? "'$cad'" : 'null');
    }

}

?>